package com.junt.mediaselector;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.junt.library.MediaSelector;
import com.junt.library.core.QueryType;
import com.junt.library.interfaces.ImageEngine;
import com.junt.library.interfaces.OnMediaSelectedListener;
import com.junt.library.model.Media;
import com.junt.library.model.MediaFormats;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.rbImages)
    RadioButton rbImages;
    @BindView(R.id.rbVideo)
    RadioButton rbVideo;
    @BindView(R.id.rbAudio)
    RadioButton rbAudio;
    @BindView(R.id.rbPNG)
    RadioButton rbPNG;
    @BindView(R.id.rbJPG)
    RadioButton rbJPG;
    @BindView(R.id.rbGIF)
    RadioButton rbGIF;
    @BindView(R.id.rbWEBP)
    RadioButton rbWEBP;
    @BindView(R.id.rgImage)
    RadioGroup rgImage;
    @BindView(R.id.rbMP4)
    RadioButton rbMP4;
    @BindView(R.id.rbMKV)
    RadioButton rbMKV;
    @BindView(R.id.rb3GP)
    RadioButton rb3GP;
    @BindView(R.id.rbWEBM)
    RadioButton rbWEBM;
    @BindView(R.id.rgVideo)
    RadioGroup rgVideo;
    @BindView(R.id.rbMP3)
    RadioButton rbMP3;
    @BindView(R.id.rbWAV)
    RadioButton rbWAV;
    @BindView(R.id.rbM4A)
    RadioButton rbM4A;
    @BindView(R.id.rbAAC)
    RadioButton rbAAC;
    @BindView(R.id.rgAudio)
    RadioGroup rgAudio;
    @BindView(R.id.etSize)
    EditText etSize;
    @BindView(R.id.etMaxSelected)
    EditText etMaxSelected;
    @BindView(R.id.btnSelect)
    Button btnSelect;
    @BindView(R.id.btnSelectDir)
    Button btnSelectDir;
    @BindView(R.id.rgType)
    RadioGroup rgType;
    @BindView(R.id.rbGlide)
    RadioButton rbGlide;
    @BindView(R.id.rbPicasso)
    RadioButton rbPicasso;
    @BindView(R.id.rgEngine)
    RadioGroup rgEngine;
    private ImageEngine imageEngine = new ImageGlideEngine();
    private QueryType queryType = QueryType.IMAGE;
    private String imageFormats = MediaFormats.NONE, audioFormats = MediaFormats.NONE, videoFormats = MediaFormats.NONE;
    private int minSize = 200;
    private int maxSelected = 9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        int granted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (granted == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 99);
        }
        initView();
    }

    private void initView() {
        rgType.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbImages.getId()) {
                queryType = QueryType.IMAGE;
            } else if (checkedId == rbAudio.getId()) {
                queryType = QueryType.AUDIO;
            } else {
                queryType = QueryType.VIDEO;
            }
        });
        rgImage.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbPNG.getId()) {
                imageFormats = MediaFormats.Image.PNG;
            } else if (checkedId == rbJPG.getId()) {
                imageFormats = MediaFormats.Image.JPEG;
            } else if (checkedId == rbGIF.getId()) {
                imageFormats = MediaFormats.Image.GIF;
            } else {
                imageFormats = MediaFormats.Image.WebP;
            }
        });
        rgAudio.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbMP3.getId()) {
                audioFormats = MediaFormats.Audio.MP3;
            } else if (checkedId == rbWAV.getId()) {
                audioFormats = MediaFormats.Audio.WAVE;
            } else if (checkedId == rbM4A.getId()) {
                audioFormats = MediaFormats.Audio.MPEG_4A;
            } else {
                audioFormats = MediaFormats.Audio.AAC;
            }
        });
        rgVideo.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbMP4.getId()) {
                videoFormats = MediaFormats.Video.MPEG_4;
            } else if (checkedId == rbMKV.getId()) {
                imageFormats = MediaFormats.Video.Matroska;
            } else if (checkedId == rb3GP.getId()) {
                imageFormats = MediaFormats.Video._3GPP;
            } else {
                imageFormats = MediaFormats.Video.WebM;
            }
        });
        rgEngine.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbGlide.getId()) {
                imageEngine = new ImageGlideEngine();
            } else {
                imageEngine = new ImagePicassoEngine();
            }
        });

        etMaxSelected.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    maxSelected = Integer.parseInt(s.toString());
                }
            }
        });
        etSize.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    minSize = Integer.parseInt(s.toString());
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(this, "需要读写外部存储权限才能扫描媒体文件！", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }

    private void selectMedia() {
        MediaSelector.getInstance()
                .setImageEngine(imageEngine)
                .setMaxSelected(maxSelected)
                .setQueryType(queryType)
                .setMinSize(minSize)
                .setMediaFormats(getMediaFormats())
                .obtain(this, new OnMediaSelectedListener() {
                    @Override
                    public void onMediaSelected(List<Media> medias) {
                        Log.i(TAG, "onMediaSelected: ");
                    }
                });
    }

    private String getMediaFormats() {
        if (queryType == QueryType.IMAGE) {
            return imageFormats;
        }
        if (queryType == QueryType.VIDEO) {
            return videoFormats;
        }
        return audioFormats;

    }

    private void selectMediaDir() {
        MediaSelector.getInstance()
                .setImageEngine(imageEngine)
                .setMaxSelected(maxSelected)
                .setQueryType(queryType)
                .setMinSize(minSize)
                .setMediaFormats(getMediaFormats())
                .obtainDir(this, new OnMediaSelectedListener() {
                    @Override
                    public void onMediaSelected(List<Media> medias) {
                        Log.i(TAG, "onMediaSelected: ");
                    }
                });
    }

    @OnClick({R.id.btnSelect, R.id.btnSelectDir})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnSelect:
                selectMedia();
                break;
            case R.id.btnSelectDir:
                selectMediaDir();
                break;
        }
    }
}